<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12"> </el-col>
				<el-col :span="12"> </el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="会议室名称" prop="name">
						<el-input v-model="dataForm.name" placeholder="会议室名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="所在地点" prop="siteId">
						<el-tree-select
							v-model="dataForm.siteId"
							:data="workplaceList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'workplace', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="所在城市" prop="cityStr">
						<el-input v-model="dataForm.cityStr" placeholder="所在城市"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="具体地址" prop="siteStr">
						<el-input v-model="dataForm.siteStr" placeholder="具体地址"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="最多容纳人数" prop="maxNumber">
				<el-input v-model="dataForm.maxNumber" placeholder="最多容纳人数" type="number">
					<template #append>人</template>
				</el-input>
			</el-form-item>
			<el-form-item label="设备" prop="equipment">
				<el-input v-model="dataForm.equipment" placeholder="设备"></el-input>
			</el-form-item>
			<el-form-item label="上传照片" prop="imgUrl">
				<UploadImgView v-model="dataForm.imgUrl"></UploadImgView>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="可用范围" prop="isAll">
				<el-switch
					v-model="dataForm.isAll"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="全员"
					inline-prompt
					inactive-text="指定"
					placeholder=""
				/>
			</el-form-item>
			<el-form-item label="允许预定" prop="isReserve">
				<el-switch
					v-model="dataForm.isReserve"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="启用"
					inline-prompt
					inactive-text="停用"
					placeholder=""
				/>
			</el-form-item>
			<el-form-item label="预定需审批" prop="isExamine">
				<el-switch
					v-model="dataForm.isExamine"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="启用"
					inline-prompt
					inactive-text="停用"
					placeholder=""
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useChamberApi, useChamberSubmitApi } from '@/api/nb/chamber/chamber'
import UploadImgView from '@/views/nb_module/image/upload-img-view.vue'
import { findAllApi } from '@/api/nb/organization/workplace'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	name: '',
	siteId: '',
	cityStr: '',
	siteStr: '',
	maxNumber: '',
	equipment: '',
	imgUrl: '',
	remark: '',
	isAll: '1',
	isReserve: '1',
	isExamine: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getChamber(id)
	}

	getWorkplace()
}

const getChamber = (id: number) => {
	useChamberApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const workplaceList=ref([])
// 获取 工作地点
const getWorkplace = () => {
	return findAllApi().then(res => {
		workplaceList.value = res.data
	})
}

const dataRules = ref({})
// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useChamberSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
